// Web Worker：在后台线程执行 TSP 计算，避免阻塞 UI
import { heldKarpPath, gaTwoOpt } from '../utils/tsp'

self.onmessage = (e) => {
  try {
    const { points, start } = e.data || {}
    if (!Array.isArray(points) || !start) {
      self.postMessage({ error: 'invalid_params' })
      return
    }
    let order = []
    // 计算出执行时间
    console.time('heldKarpPath')
    if (points.length <= 15) {
      order = heldKarpPath(points, start)
    } else {
      // 降低种群与代数，进一步减少算力占用
      order = gaTwoOpt(points, start, { popSize: 60, generations: 120, mutationRate: 0.15 })
    }
    console.timeEnd('heldKarpPath')
    self.postMessage({ order })
  } catch (err) {
    self.postMessage({ error: String(err && err.message ? err.message : err) })
  }
}


